#! 
# by jzhang @ 16:18 2004-09-27  mailto:jzhang533@gmail.com
#
package Google_Search;
use SOAP::Lite;
use Encode;

my @Exporter = qw(new set search);

sub new
{
    my $class = shift;
    my %p = @_;
    my $defaults = {
        'licence_key'       => '5RMp7/BQFHL+KFdaq6J2hh3C1D79kxRC',
        'wsdl_file'         => './GoogleSearch.wsdl',
        'retry_times'       => 30,
        'language_restrict' => "",
        'restrict'          => "",
        'verbose'           => 0
    };
    $defaults->{licence_key}       = $p{licence_key} if exists $p{licence_key};
    $defaults->{wsdl_file}         = $p{wsdl_file} if exists $p{wsdl_file};
    $defaults->{retry_times}       = $p{retry_times} if exists $p{retry_times};
    $defaults->{language_restrict} = $p{language_restrict} if exists $p{language_restrict};
    $defaults->{restrict}          = $p{restrict} if exists $p{restrict};
    $defaults->{verbose}           = $p{verbose} if exists $p{verbose};
    
    $defaults->{google_search} = SOAP::Lite->service("file:".$defaults->{wsdl_file});
    return undef if not defined $defaults->{google_search};
    bless $defaults,$class;
}

sub set
{
    my $this  = shift;
    my $key   = shift;
    my $value = shift;
    $this->{$key} = $value if exists $this->{$key};    
}


sub search
{
#    print "come in...\n";
    my $this = shift;
    my $query = shift;
    my $max_results = 10;
    $max_results = shift if (@_);
    my @urls;

    $query = decode("gb2312",$query);
    my $results;
    my $result;
    my $numbers;
    my $count = 0;
    my $retry_count = 0;

    my $google_search = $this->{google_search};
    while ($count < $max_results)
    {
        if($this->{verbose}){  print "doing google search by query: \"",encode("gb2312",$query),"\"\n";}
      Retry:
        eval{
            $results = $google_search -> doGoogleSearch(
                $this->{licence_key},
                $query,
                $count,
                10,
                "false",
                $this->{restrict},
                "false",
                $this->{language_restrict},
                "",
                "");
        };
        if($@){
            $retry_count ++;
            if($retry_count > $this->{retry_times})
            {
                if($this->{verbose}){print "$retry_count, max retrying reached\n";}
                last;
            }
            print "retrying...attempt $retry_count, :$!\n" if($this->{verbose});
            goto Retry;
        }
        $retry_count = 0;
        
        
        $numbers = @{$results->{resultElements}};
        $numbers = 0 if not defined $numbers;
        print $numbers," results returned in this query\n" if($this->{verbose});
        
        if( $numbers < 10 ) {
            foreach $result (@{$results->{resultElements}}) {
                push @urls,$result->{URL};
                $count++;
                if( $count>=$max_results)
                {
                    print "max count reached\n" if($this->{verbose});
                    last;
                }
            }
            last;
        }
        else {
            foreach $result (@{$results->{resultElements}}) {
                push @urls,$result->{URL};
                $count++;
                if( $count>=$max_results)
                {
                    print "max count reached\n" if($this->{verbose});
                    last;
                }
            }
        }
    }
    return @urls;
}

1;
